package com.ruoyi.web.controller.message;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.message.domain.MiniMessage;
import com.ruoyi.system.message.service.IMiniMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 消息提醒
 *
 * @author xzw
 * @date 2024-02-18
 */
@RestController
@RequestMapping("/system/message")
public class MiniMessageController extends BaseController {
    @Autowired
    private IMiniMessageService miniMessageService;

    /**
     * 查询消息提醒列表
     */
    //@PreAuthorize("@ss.hasPermi('system:message:list')")
    @GetMapping("/list")
    public TableDataInfo list(MiniMessage miniMessage) {
        List<MiniMessage> list = miniMessageService.selectMiniMessageList(miniMessage);
        return getDataTable(list);
    }

    /**
     * 导出消息提醒列表
     */
    //@PreAuthorize("@ss.hasPermi('system:message:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, MiniMessage miniMessage) {
        List<MiniMessage> list = miniMessageService.selectMiniMessageList(miniMessage);
        ExcelUtil<MiniMessage> util = new ExcelUtil<MiniMessage>(MiniMessage.class);
        util.exportExcel(response, list, "消息提醒数据");
    }

    /**
     * 获取消息提醒详细信息
     */
    //@PreAuthorize("@ss.hasPermi('system:message:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(miniMessageService.selectMiniMessageById(id));
    }

    /**
     * 新增消息提醒
     */
    //@PreAuthorize("@ss.hasPermi('system:message:add')")
    @PostMapping
    public AjaxResult add(@RequestBody MiniMessage miniMessage) {
        return toAjax(miniMessageService.insertMiniMessage(miniMessage));
    }

    /**
     * 修改消息提醒
     */
    //@PreAuthorize("@ss.hasPermi('system:message:edit')")
    @PutMapping
    public AjaxResult edit(@RequestBody MiniMessage miniMessage) {
        return toAjax(miniMessageService.updateMiniMessage(miniMessage));
    }

    /**
     * 删除消息提醒
     */
    //@PreAuthorize("@ss.hasPermi('system:message:remove')")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(miniMessageService.deleteMiniMessageByIds(ids));
    }

    /**
     * 未读消息数量
     * @return
     * @date 2024/03/27 04:1547
     * @author lihj
     */
    @GetMapping("unreadCount")
    public AjaxResult unreadCount() {
        return success(miniMessageService.unreadCount(SecurityUtils.getUserId()));
    }

    /**
     * 一键已读
     *
     * @return
     */
    @GetMapping("oneClickRead")
    public AjaxResult oneClickRead() {
        return toAjax((miniMessageService.oneClickRead(SecurityUtils.getUserId())));
    }
}
